﻿using System;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYPipeSpecification : IComparable, IHYXmlSerial, IHYSpecification
	{
		public object Tag { get; set; }

		public int DN { get; set; }

		public double Diameter { get; set; }

		public int MaterialId { get; set; }

		public double OutDiameter { get; set; }

		public double Thickness { get; set; }

		public double Factor
		{
			get
			{
				return 1.0;
			}
		}

		public double Area
		{
			get
			{
				return GeUtils.Area_Round(this.Diameter / 1000.0);
			}
		}

		public double Circumference
		{
			get
			{
				return GeUtils.Circumference_Round(this.Diameter / 1000.0);
			}
		}

		public double Rs
		{
			get
			{
				return this.Diameter / 4000.0;
			}
		}

		public double D
		{
			get
			{
				return this.Diameter / 1000.0;
			}
		}

		public bool IsValid
		{
			get
			{
				return this.DN > 0 && this.Diameter > 0.0;
			}
		}

		public Range VelocityRange
		{
			get
			{
				return this.m_VelocityRange;
			}
		}

		public Range LoadRange
		{
			get
			{
				return this.m_LoadRange;
			}
		}

		public Range RmRange
		{
			get
			{
				return this.m_RmRange;
			}
		}

		public int CompareTo(object obj)
		{
			if (obj is HYPipeSpecification)
			{
				HYPipeSpecification hypipeSpecification = obj as HYPipeSpecification;
				return this.DN - hypipeSpecification.DN;
			}
			return 1;
		}

		public static bool operator <(HYPipeSpecification left, HYPipeSpecification right)
		{
			return left.DN < right.DN;
		}

		public static bool operator >(HYPipeSpecification left, HYPipeSpecification right)
		{
			return left.DN > right.DN;
		}

		public void WriteXml(XElement xe)
		{
			xe.SetAttributeValue("DN", this.DN);
			xe.SetAttributeValue("MaterialId", this.MaterialId);
			xe.SetAttributeValue("Diameter", this.Diameter);
			xe.SetAttributeValue("OutDiameter", this.OutDiameter);
			xe.SetAttributeValue("Thickness", this.Thickness);
			XElement xelement = xe.Element("VelocityRange");
			if (xelement == null)
			{
				xelement = new XElement("VelocityRange");
				xe.Add(xelement);
			}
			this.m_VelocityRange.WriteXml(xelement);
			XElement xelement2 = xe.Element("LoadRange");
			if (xelement2 == null)
			{
				xelement2 = new XElement("LoadRange");
				xe.Add(xelement2);
			}
			this.m_LoadRange.WriteXml(xelement2);
			XElement xelement3 = xe.Element("RmRange");
			if (xelement3 == null)
			{
				xelement3 = new XElement("RmRange");
				xe.Add(xelement3);
			}
			this.m_RmRange.WriteXml(xelement3);
		}

		public void ReadXml(XElement xe)
		{
			this.DN = (int)xe.Attribute("DN");
			this.MaterialId = (int)xe.Attribute("MaterialId");
			this.Diameter = (double)xe.Attribute("Diameter");
			this.OutDiameter = (double)xe.Attribute("OutDiameter");
			this.Thickness = (double)xe.Attribute("Thickness");
			XElement xelement = xe.Element("VelocityRange");
			if (xelement != null)
			{
				this.m_VelocityRange.ReadXml(xelement);
			}
			XElement xelement2 = xe.Element("LoadRange");
			if (xelement2 != null)
			{
				this.m_LoadRange.ReadXml(xelement2);
			}
			XElement xelement3 = xe.Element("RmRange");
			if (xelement3 != null)
			{
				this.m_RmRange.ReadXml(xelement3);
			}
		}

		public double GetVelocity(double flow)
		{
			return Hydrokinetics.Velocity(flow, this.Area);
		}

		private Range m_VelocityRange = new Range(0.0, 0.0);

		private Range m_LoadRange = new Range(0.0, 5000.0);

		private Range m_RmRange = new Range(100.0, 300.0);
	}
}
